<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>networkx.classes.graph &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for networkx.classes.graph</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Base class for undirected graphs.</span>

<span class="sd">The Graph class allows any hashable object as a node</span>
<span class="sd">and can associate key/value attribute pairs with each undirected edge.</span>

<span class="sd">Self-loops are allowed but multiple edges are not (see MultiGraph).</span>

<span class="sd">For directed graphs see DiGraph and MultiDiGraph.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="c1">#    Copyright (C) 2004-2015 by</span>
<span class="c1">#    Aric Hagberg &lt;hagberg@lanl.gov&gt;</span>
<span class="c1">#    Dan Schult &lt;dschult@colgate.edu&gt;</span>
<span class="c1">#    Pieter Swart &lt;swart@lanl.gov&gt;</span>
<span class="c1">#    All rights reserved.</span>
<span class="c1">#    BSD license.</span>
<span class="kn">from</span> <span class="nn">copy</span> <span class="k">import</span> <span class="n">deepcopy</span>
<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>
<span class="kn">from</span> <span class="nn">networkx.exception</span> <span class="k">import</span> <span class="n">NetworkXError</span>
<span class="kn">import</span> <span class="nn">networkx.convert</span> <span class="k">as</span> <span class="nn">convert</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span><span class="se">\n</span><span class="s2">&quot;&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s1">&#39;Aric Hagberg (hagberg@lanl.gov)&#39;</span><span class="p">,</span>
                            <span class="s1">&#39;Pieter Swart (swart@lanl.gov)&#39;</span><span class="p">,</span>
                            <span class="s1">&#39;Dan Schult(dschult@colgate.edu)&#39;</span><span class="p">])</span>


<span class="k">class</span> <span class="nc">Graph</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base class for undirected graphs.</span>

<span class="sd">    A Graph stores nodes and edges with optional data, or attributes.</span>

<span class="sd">    Graphs hold undirected edges.  Self loops are allowed but multiple</span>
<span class="sd">    (parallel) edges are not.</span>

<span class="sd">    Nodes can be arbitrary (hashable) Python objects with optional</span>
<span class="sd">    key/value attributes.</span>

<span class="sd">    Edges are represented as links between nodes with optional</span>
<span class="sd">    key/value attributes.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    data : input graph</span>
<span class="sd">        Data to initialize graph.  If data=None (default) an empty</span>
<span class="sd">        graph is created.  The data can be an edge list, or any</span>
<span class="sd">        NetworkX graph object.  If the corresponding optional Python</span>
<span class="sd">        packages are installed the data can also be a NumPy matrix</span>
<span class="sd">        or 2d ndarray, a SciPy sparse matrix, or a PyGraphviz graph.</span>

<span class="sd">    attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">        Attributes to add to graph as key=value pairs.</span>

<span class="sd">    See Also</span>
<span class="sd">    --------</span>
<span class="sd">    DiGraph</span>
<span class="sd">    MultiGraph</span>
<span class="sd">    MultiDiGraph</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    Create an empty graph structure (a &quot;null graph&quot;) with no nodes and</span>
<span class="sd">    no edges.</span>

<span class="sd">    &gt;&gt;&gt; G = nx.Graph()</span>

<span class="sd">    G can be grown in several ways.</span>

<span class="sd">    **Nodes:**</span>

<span class="sd">    Add one node at a time:</span>

<span class="sd">    &gt;&gt;&gt; G.add_node(1)</span>

<span class="sd">    Add the nodes from any container (a list, dict, set or</span>
<span class="sd">    even the lines from a file or the nodes from another graph).</span>

<span class="sd">    &gt;&gt;&gt; G.add_nodes_from([2,3])</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from(range(100,110))</span>
<span class="sd">    &gt;&gt;&gt; H=nx.Graph()</span>
<span class="sd">    &gt;&gt;&gt; H.add_path([0,1,2,3,4,5,6,7,8,9])</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from(H)</span>

<span class="sd">    In addition to strings and integers any hashable Python object</span>
<span class="sd">    (except None) can represent a node, e.g. a customized node object,</span>
<span class="sd">    or even another Graph.</span>

<span class="sd">    &gt;&gt;&gt; G.add_node(H)</span>

<span class="sd">    **Edges:**</span>

<span class="sd">    G can also be grown by adding edges.</span>

<span class="sd">    Add one edge,</span>

<span class="sd">    &gt;&gt;&gt; G.add_edge(1, 2)</span>

<span class="sd">    a list of edges,</span>

<span class="sd">    &gt;&gt;&gt; G.add_edges_from([(1,2),(1,3)])</span>

<span class="sd">    or a collection of edges,</span>

<span class="sd">    &gt;&gt;&gt; G.add_edges_from(H.edges())</span>

<span class="sd">    If some edges connect nodes not yet in the graph, the nodes</span>
<span class="sd">    are added automatically.  There are no errors when adding</span>
<span class="sd">    nodes or edges that already exist.</span>

<span class="sd">    **Attributes:**</span>

<span class="sd">    Each graph, node, and edge can hold key/value attribute pairs</span>
<span class="sd">    in an associated attribute dictionary (the keys must be hashable).</span>
<span class="sd">    By default these are empty, but can be added or changed using</span>
<span class="sd">    add_edge, add_node or direct manipulation of the attribute</span>
<span class="sd">    dictionaries named graph, node and edge respectively.</span>

<span class="sd">    &gt;&gt;&gt; G = nx.Graph(day=&quot;Friday&quot;)</span>
<span class="sd">    &gt;&gt;&gt; G.graph</span>
<span class="sd">    {&#39;day&#39;: &#39;Friday&#39;}</span>

<span class="sd">    Add node attributes using add_node(), add_nodes_from() or G.node</span>

<span class="sd">    &gt;&gt;&gt; G.add_node(1, time=&#39;5pm&#39;)</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from([3], time=&#39;2pm&#39;)</span>
<span class="sd">    &gt;&gt;&gt; G.node[1]</span>
<span class="sd">    {&#39;time&#39;: &#39;5pm&#39;}</span>
<span class="sd">    &gt;&gt;&gt; G.node[1][&#39;room&#39;] = 714</span>
<span class="sd">    &gt;&gt;&gt; del G.node[1][&#39;room&#39;] # remove attribute</span>
<span class="sd">    &gt;&gt;&gt; G.nodes(data=True)</span>
<span class="sd">    [(1, {&#39;time&#39;: &#39;5pm&#39;}), (3, {&#39;time&#39;: &#39;2pm&#39;})]</span>

<span class="sd">    Warning: adding a node to G.node does not add it to the graph.</span>

<span class="sd">    Add edge attributes using add_edge(), add_edges_from(), subscript</span>
<span class="sd">    notation, or G.edge.</span>

<span class="sd">    &gt;&gt;&gt; G.add_edge(1, 2, weight=4.7 )</span>
<span class="sd">    &gt;&gt;&gt; G.add_edges_from([(3,4),(4,5)], color=&#39;red&#39;)</span>
<span class="sd">    &gt;&gt;&gt; G.add_edges_from([(1,2,{&#39;color&#39;:&#39;blue&#39;}), (2,3,{&#39;weight&#39;:8})])</span>
<span class="sd">    &gt;&gt;&gt; G[1][2][&#39;weight&#39;] = 4.7</span>
<span class="sd">    &gt;&gt;&gt; G.edge[1][2][&#39;weight&#39;] = 4</span>

<span class="sd">    **Shortcuts:**</span>

<span class="sd">    Many common graph features allow python syntax to speed reporting.</span>

<span class="sd">    &gt;&gt;&gt; 1 in G     # check if node in graph</span>
<span class="sd">    True</span>
<span class="sd">    &gt;&gt;&gt; [n for n in G if n&lt;3]   # iterate through nodes</span>
<span class="sd">    [1, 2]</span>
<span class="sd">    &gt;&gt;&gt; len(G)  # number of nodes in graph</span>
<span class="sd">    5</span>

<span class="sd">    The fastest way to traverse all edges of a graph is via</span>
<span class="sd">    adjacency_iter(), but the edges() method is often more convenient.</span>

<span class="sd">    &gt;&gt;&gt; for n,nbrsdict in G.adjacency_iter():</span>
<span class="sd">    ...     for nbr,eattr in nbrsdict.items():</span>
<span class="sd">    ...        if &#39;weight&#39; in eattr:</span>
<span class="sd">    ...            (n,nbr,eattr[&#39;weight&#39;])</span>
<span class="sd">    (1, 2, 4)</span>
<span class="sd">    (2, 1, 4)</span>
<span class="sd">    (2, 3, 8)</span>
<span class="sd">    (3, 2, 8)</span>
<span class="sd">    &gt;&gt;&gt; G.edges(data=&#39;weight&#39;)</span>
<span class="sd">    [(1, 2, 4), (2, 3, 8), (3, 4, None), (4, 5, None)]</span>

<span class="sd">    **Reporting:**</span>

<span class="sd">    Simple graph information is obtained using methods.</span>
<span class="sd">    Iterator versions of many reporting methods exist for efficiency.</span>
<span class="sd">    Methods exist for reporting nodes(), edges(), neighbors() and degree()</span>
<span class="sd">    as well as the number of nodes and edges.</span>

<span class="sd">    For details on these and other miscellaneous methods, see below.</span>

<span class="sd">    **Subclasses (Advanced):**</span>

<span class="sd">    The Graph class uses a dict-of-dict-of-dict data structure.</span>
<span class="sd">    The outer dict (node_dict) holds adjacency lists keyed by node.</span>
<span class="sd">    The next dict (adjlist) represents the adjacency list and holds</span>
<span class="sd">    edge data keyed by neighbor.  The inner dict (edge_attr) represents</span>
<span class="sd">    the edge data and holds edge attribute values keyed by attribute names.</span>

<span class="sd">    Each of these three dicts can be replaced by a user defined</span>
<span class="sd">    dict-like object. In general, the dict-like features should be</span>
<span class="sd">    maintained but extra features can be added. To replace one of the</span>
<span class="sd">    dicts create a new graph class by changing the class(!) variable</span>
<span class="sd">    holding the factory for that dict-like structure. The variable names</span>
<span class="sd">    are node_dict_factory, adjlist_dict_factory and edge_attr_dict_factory.</span>

<span class="sd">    node_dict_factory : function, (default: dict)</span>
<span class="sd">        Factory function to be used to create the outer-most dict</span>
<span class="sd">        in the data structure that holds adjacency lists keyed by node.</span>
<span class="sd">        It should require no arguments and return a dict-like object.</span>

<span class="sd">    adjlist_dict_factory : function, (default: dict)</span>
<span class="sd">        Factory function to be used to create the adjacency list</span>
<span class="sd">        dict which holds edge data keyed by neighbor.</span>
<span class="sd">        It should require no arguments and return a dict-like object</span>

<span class="sd">    edge_attr_dict_factory : function, (default: dict)</span>
<span class="sd">        Factory function to be used to create the edge attribute</span>
<span class="sd">        dict which holds attrbute values keyed by attribute name.</span>
<span class="sd">        It should require no arguments and return a dict-like object.</span>

<span class="sd">    Examples</span>
<span class="sd">    --------</span>
<span class="sd">    Create a graph object that tracks the order nodes are added.</span>

<span class="sd">    &gt;&gt;&gt; from collections import OrderedDict</span>
<span class="sd">    &gt;&gt;&gt; class OrderedNodeGraph(nx.Graph):</span>
<span class="sd">    ...     node_dict_factory=OrderedDict</span>
<span class="sd">    &gt;&gt;&gt; G=OrderedNodeGraph()</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from( (2,1) )</span>
<span class="sd">    &gt;&gt;&gt; G.nodes()</span>
<span class="sd">    [2, 1]</span>
<span class="sd">    &gt;&gt;&gt; G.add_edges_from( ((2,2), (2,1), (1,1)) )</span>
<span class="sd">    &gt;&gt;&gt; G.edges()</span>
<span class="sd">    [(2, 1), (2, 2), (1, 1)]</span>

<span class="sd">    Create a graph object that tracks the order nodes are added</span>
<span class="sd">    and for each node track the order that neighbors are added.</span>

<span class="sd">    &gt;&gt;&gt; class OrderedGraph(nx.Graph):</span>
<span class="sd">    ...    node_dict_factory = OrderedDict</span>
<span class="sd">    ...    adjlist_dict_factory = OrderedDict</span>
<span class="sd">    &gt;&gt;&gt; G = OrderedGraph()</span>
<span class="sd">    &gt;&gt;&gt; G.add_nodes_from( (2,1) )</span>
<span class="sd">    &gt;&gt;&gt; G.nodes()</span>
<span class="sd">    [2, 1]</span>
<span class="sd">    &gt;&gt;&gt; G.add_edges_from( ((2,2), (2,1), (1,1)) )</span>
<span class="sd">    &gt;&gt;&gt; G.edges()</span>
<span class="sd">    [(2, 2), (2, 1), (1, 1)]</span>

<span class="sd">    Create a low memory graph class that effectively disallows edge</span>
<span class="sd">    attributes by using a single attribute dict for all edges.</span>
<span class="sd">    This reduces the memory used, but you lose edge attributes.</span>

<span class="sd">    &gt;&gt;&gt; class ThinGraph(nx.Graph):</span>
<span class="sd">    ...     all_edge_dict = {&#39;weight&#39;: 1}</span>
<span class="sd">    ...     def single_edge_dict(self):</span>
<span class="sd">    ...         return self.all_edge_dict</span>
<span class="sd">    ...     edge_attr_dict_factory = single_edge_dict</span>
<span class="sd">    &gt;&gt;&gt; G = ThinGraph()</span>
<span class="sd">    &gt;&gt;&gt; G.add_edge(2,1)</span>
<span class="sd">    &gt;&gt;&gt; G.edges(data= True)</span>
<span class="sd">    [(1, 2, {&#39;weight&#39;: 1})]</span>
<span class="sd">    &gt;&gt;&gt; G.add_edge(2,2)</span>
<span class="sd">    &gt;&gt;&gt; G[2][1] is G[2][2]</span>
<span class="sd">    True</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">node_dict_factory</span> <span class="o">=</span> <span class="nb">dict</span>
    <span class="n">adjlist_dict_factory</span> <span class="o">=</span> <span class="nb">dict</span>
    <span class="n">edge_attr_dict_factory</span> <span class="o">=</span> <span class="nb">dict</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize a graph with edges, name, graph attributes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data : input graph</span>
<span class="sd">            Data to initialize graph.  If data=None (default) an empty</span>
<span class="sd">            graph is created.  The data can be an edge list, or any</span>
<span class="sd">            NetworkX graph object.  If the corresponding optional Python</span>
<span class="sd">            packages are installed the data can also be a NumPy matrix</span>
<span class="sd">            or 2d ndarray, a SciPy sparse matrix, or a PyGraphviz graph.</span>
<span class="sd">        name : string, optional (default=&#39;&#39;)</span>
<span class="sd">            An optional name for the graph.</span>
<span class="sd">        attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">            Attributes to add to graph as key=value pairs.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        convert</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph(name=&#39;my graph&#39;)</span>
<span class="sd">        &gt;&gt;&gt; e = [(1,2),(2,3),(3,4)] # list of edges</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph(e)</span>

<span class="sd">        Arbitrary graph attribute pairs (key=value) may be assigned</span>

<span class="sd">        &gt;&gt;&gt; G=nx.Graph(e, day=&quot;Friday&quot;)</span>
<span class="sd">        &gt;&gt;&gt; G.graph</span>
<span class="sd">        {&#39;day&#39;: &#39;Friday&#39;}</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">node_dict_factory</span> <span class="o">=</span> <span class="n">ndf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">node_dict_factory</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edge_attr_dict_factory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge_attr_dict_factory</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">graph</span> <span class="o">=</span> <span class="p">{}</span>   <span class="c1"># dictionary for graph attributes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">node</span> <span class="o">=</span> <span class="n">ndf</span><span class="p">()</span>  <span class="c1"># empty node attribute dict</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj</span> <span class="o">=</span> <span class="n">ndf</span><span class="p">()</span>  <span class="c1"># empty adjacency dict</span>
        <span class="c1"># attempt to load graph with data</span>
        <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">convert</span><span class="o">.</span><span class="n">to_networkx_graph</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">create_using</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
        <span class="c1"># load graph attributes (must be after convert)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">edge</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>

    <span class="nd">@name</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">s</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the graph name.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        name : string</span>
<span class="sd">            The name of the graph.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph(name=&#39;foo&#39;)</span>
<span class="sd">        &gt;&gt;&gt; str(G)</span>
<span class="sd">        &#39;foo&#39;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Iterate over the nodes. Use the expression &#39;for n in G&#39;.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        niter : iterator</span>
<span class="sd">            An iterator over all nodes in the graph.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if n is a node, False otherwise. Use the expression</span>
<span class="sd">        &#39;n in G&#39;.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; 1 in G</span>
<span class="sd">        True</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of nodes. Use the expression &#39;len(G)&#39;.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nnodes : int</span>
<span class="sd">            The number of nodes in the graph.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; len(G)</span>
<span class="sd">        4</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a dict of neighbors of node n.  Use the expression &#39;G[n]&#39;.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : node</span>
<span class="sd">           A node in the graph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        adj_dict : dictionary</span>
<span class="sd">           The adjacency dictionary for nodes connected to n.</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        G[n] is similar to G.neighbors(n) but the internal data dictionary</span>
<span class="sd">        is returned instead of a list.</span>

<span class="sd">        Assigning G[n] will corrupt the internal graph data structure.</span>
<span class="sd">        Use G[n] for reading data only.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G[0]</span>
<span class="sd">        {1: {}}</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">add_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">attr_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a single node n and update node attributes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : node</span>
<span class="sd">            A node can be any hashable Python object except None.</span>
<span class="sd">        attr_dict : dictionary, optional (default= no attributes)</span>
<span class="sd">            Dictionary of node attributes.  Key/value pairs will</span>
<span class="sd">            update existing data associated with the node.</span>
<span class="sd">        attr : keyword arguments, optional</span>
<span class="sd">            Set or change attributes using key=value.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_nodes_from</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_node(1)</span>
<span class="sd">        &gt;&gt;&gt; G.add_node(&#39;Hello&#39;)</span>
<span class="sd">        &gt;&gt;&gt; K3 = nx.Graph([(0,1),(1,2),(2,0)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_node(K3)</span>
<span class="sd">        &gt;&gt;&gt; G.number_of_nodes()</span>
<span class="sd">        3</span>

<span class="sd">        Use keywords set/change node attributes:</span>

<span class="sd">        &gt;&gt;&gt; G.add_node(1,size=10)</span>
<span class="sd">        &gt;&gt;&gt; G.add_node(3,weight=0.4,UTM=(&#39;13S&#39;,382871,3972649))</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        A hashable object is one that can be used as a key in a Python</span>
<span class="sd">        dictionary. This includes strings, numbers, tuples of strings</span>
<span class="sd">        and numbers, etc.</span>

<span class="sd">        On many platforms hashable items also include mutables such as</span>
<span class="sd">        NetworkX Graphs, though one should be careful that the hash</span>
<span class="sd">        doesn&#39;t change on mutables.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># set up attribute dict</span>
        <span class="k">if</span> <span class="n">attr_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attr_dict</span> <span class="o">=</span> <span class="n">attr</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">attr_dict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span>
                    <span class="s2">&quot;The attr_dict argument must be a dictionary.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">n</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">attr_dict</span>
        <span class="k">else</span><span class="p">:</span>  <span class="c1"># update attr even if node already exists</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr_dict</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add multiple nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nodes : iterable container</span>
<span class="sd">            A container of nodes (list, dict, set, etc.).</span>
<span class="sd">            OR</span>
<span class="sd">            A container of (node, attribute dict) tuples.</span>
<span class="sd">            Node attributes are updated using the attribute dict.</span>
<span class="sd">        attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">            Update attributes for all nodes in nodes.</span>
<span class="sd">            Node attributes specified in nodes as a tuple</span>
<span class="sd">            take precedence over attributes specified generally.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_node</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from(&#39;Hello&#39;)</span>
<span class="sd">        &gt;&gt;&gt; K3 = nx.Graph([(0,1),(1,2),(2,0)])</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from(K3)</span>
<span class="sd">        &gt;&gt;&gt; sorted(G.nodes(),key=str)</span>
<span class="sd">        [0, 1, 2, &#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;o&#39;]</span>

<span class="sd">        Use keywords to update specific node attributes for every node.</span>

<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([1,2], size=10)</span>
<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([3,4], weight=0.4)</span>

<span class="sd">        Use (node, attrdict) tuples to update attributes for specific</span>
<span class="sd">        nodes.</span>

<span class="sd">        &gt;&gt;&gt; G.add_nodes_from([(1,dict(size=11)), (2,{&#39;color&#39;:&#39;blue&#39;})])</span>
<span class="sd">        &gt;&gt;&gt; G.node[1][&#39;size&#39;]</span>
<span class="sd">        11</span>
<span class="sd">        &gt;&gt;&gt; H = nx.Graph()</span>
<span class="sd">        &gt;&gt;&gt; H.add_nodes_from(G.nodes(data=True))</span>
<span class="sd">        &gt;&gt;&gt; H.node[1][&#39;size&#39;]</span>
<span class="sd">        11</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span>
            <span class="c1"># keep all this inside try/except because</span>
            <span class="c1"># CPython throws TypeError on n not in self.succ,</span>
            <span class="c1"># while pre-2.7.5 ironpython throws on self.succ[n]</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">n</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
                <span class="n">nn</span><span class="p">,</span> <span class="n">ndict</span> <span class="o">=</span> <span class="n">n</span>
                <span class="k">if</span> <span class="n">nn</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">nn</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
                    <span class="n">newdict</span> <span class="o">=</span> <span class="n">attr</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                    <span class="n">newdict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">ndict</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">nn</span><span class="p">]</span> <span class="o">=</span> <span class="n">newdict</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">olddict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">nn</span><span class="p">]</span>
                    <span class="n">olddict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
                    <span class="n">olddict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">ndict</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">remove_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove node n.</span>

<span class="sd">        Removes the node n and all adjacent edges.</span>
<span class="sd">        Attempting to remove a non-existent node will raise an exception.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : node</span>
<span class="sd">           A node in the graph</span>

<span class="sd">        Raises</span>
<span class="sd">        -------</span>
<span class="sd">        NetworkXError</span>
<span class="sd">           If n is not in the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        remove_nodes_from</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; G.edges()</span>
<span class="sd">        [(0, 1), (1, 2)]</span>
<span class="sd">        &gt;&gt;&gt; G.remove_node(1)</span>
<span class="sd">        &gt;&gt;&gt; G.edges()</span>
<span class="sd">        []</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">adj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">nbrs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>  <span class="c1"># keys handles self-loops (allow mutation later)</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>  <span class="c1"># NetworkXError if n not in self</span>
            <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span><span class="s2">&quot;The node </span><span class="si">%s</span><span class="s2"> is not in the graph.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,))</span>
        <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">n</span><span class="p">]</span>   <span class="c1"># remove all edges n-u in graph</span>
        <span class="k">del</span> <span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>          <span class="c1"># now remove node</span>

    <span class="k">def</span> <span class="nf">remove_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove multiple nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nodes : iterable container</span>
<span class="sd">            A container of nodes (list, dict, set, etc.).  If a node</span>
<span class="sd">            in the container is not in the graph it is silently</span>
<span class="sd">            ignored.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        remove_node</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; e = G.nodes()</span>
<span class="sd">        &gt;&gt;&gt; e</span>
<span class="sd">        [0, 1, 2]</span>
<span class="sd">        &gt;&gt;&gt; G.remove_nodes_from(e)</span>
<span class="sd">        &gt;&gt;&gt; G.nodes()</span>
<span class="sd">        []</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">adj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">u</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>   <span class="c1"># keys() handles self-loops</span>
                    <span class="k">del</span> <span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">n</span><span class="p">]</span>  <span class="c1"># (allows mutation of dict in loop)</span>
                <span class="k">del</span> <span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">nodes_iter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iterator over the nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data : boolean, optional (default=False)</span>
<span class="sd">               If False the iterator returns nodes.  If True</span>
<span class="sd">               return a two-tuple of node and node data dictionary</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        niter : iterator</span>
<span class="sd">            An iterator over nodes.  If data=True the iterator gives</span>
<span class="sd">            two-tuples containing (node, node data, dictionary)</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        If the node data is not required it is simpler and equivalent</span>
<span class="sd">        to use the expression &#39;for n in G&#39;.</span>

<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>

<span class="sd">        &gt;&gt;&gt; [d for n,d in G.nodes_iter(data=True)]</span>
<span class="sd">        [{}, {}, {}]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">data</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a list of the nodes in the graph.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data : boolean, optional (default=False)</span>
<span class="sd">               If False return a list of nodes.  If True return a</span>
<span class="sd">               two-tuple of node and node data dictionary</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nlist : list</span>
<span class="sd">            A list of nodes.  If data=True a list of two-tuples containing</span>
<span class="sd">            (node, node data dictionary).</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; G.nodes()</span>
<span class="sd">        [0, 1, 2]</span>
<span class="sd">        &gt;&gt;&gt; G.add_node(1, time=&#39;5pm&#39;)</span>
<span class="sd">        &gt;&gt;&gt; G.nodes(data=True)</span>
<span class="sd">        [(0, {}), (1, {&#39;time&#39;: &#39;5pm&#39;}), (2, {})]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">nodes_iter</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">number_of_nodes</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of nodes in the graph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nnodes : int</span>
<span class="sd">            The number of nodes in the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        order, __len__  which are identical</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; len(G)</span>
<span class="sd">        3</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">order</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of nodes in the graph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nnodes : int</span>
<span class="sd">            The number of nodes in the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        number_of_nodes, __len__  which are identical</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">has_node</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the graph contains the node n.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : node</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; G.has_node(0)</span>
<span class="sd">        True</span>

<span class="sd">        It is more readable and simpler to use</span>

<span class="sd">        &gt;&gt;&gt; 0 in G</span>
<span class="sd">        True</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">add_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">attr_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add an edge between u and v.</span>

<span class="sd">        The nodes u and v will be automatically added if they are</span>
<span class="sd">        not already in the graph.</span>

<span class="sd">        Edge attributes can be specified with keywords or by providing</span>
<span class="sd">        a dictionary with key/value pairs.  See examples below.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v : nodes</span>
<span class="sd">            Nodes can be, for example, strings or numbers.</span>
<span class="sd">            Nodes must be hashable (and not None) Python objects.</span>
<span class="sd">        attr_dict : dictionary, optional (default= no attributes)</span>
<span class="sd">            Dictionary of edge attributes.  Key/value pairs will</span>
<span class="sd">            update existing data associated with the edge.</span>
<span class="sd">        attr : keyword arguments, optional</span>
<span class="sd">            Edge data (or labels or objects) can be assigned using</span>
<span class="sd">            keyword arguments.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_edges_from : add a collection of edges</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Adding an edge that already exists updates the edge data.</span>

<span class="sd">        Many NetworkX algorithms designed for weighted graphs use as</span>
<span class="sd">        the edge weight a numerical value assigned to a keyword</span>
<span class="sd">        which by default is &#39;weight&#39;.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        The following all add the edge e=(1,2) to graph G:</span>

<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; e = (1,2)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1, 2)           # explicit two-node form</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(*e)             # single edge as tuple of two nodes</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from( [(1,2)] ) # add edges from iterable container</span>

<span class="sd">        Associate data to edges using keywords:</span>

<span class="sd">        &gt;&gt;&gt; G.add_edge(1, 2, weight=3)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1, 3, weight=7, capacity=15, length=342.7)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># set up attribute dictionary</span>
        <span class="k">if</span> <span class="n">attr_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attr_dict</span> <span class="o">=</span> <span class="n">attr</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">attr_dict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span>
                    <span class="s2">&quot;The attr_dict argument must be a dictionary.&quot;</span><span class="p">)</span>
        <span class="c1"># add nodes</span>
        <span class="k">if</span> <span class="n">u</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1"># add the edge</span>
        <span class="n">datadict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge_attr_dict_factory</span><span class="p">())</span>
        <span class="n">datadict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr_dict</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">datadict</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="n">datadict</span>

    <span class="k">def</span> <span class="nf">add_edges_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ebunch</span><span class="p">,</span> <span class="n">attr_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add all the edges in ebunch.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        ebunch : container of edges</span>
<span class="sd">            Each edge given in the container will be added to the</span>
<span class="sd">            graph. The edges must be given as as 2-tuples (u,v) or</span>
<span class="sd">            3-tuples (u,v,d) where d is a dictionary containing edge</span>
<span class="sd">            data.</span>
<span class="sd">        attr_dict : dictionary, optional (default= no attributes)</span>
<span class="sd">            Dictionary of edge attributes.  Key/value pairs will</span>
<span class="sd">            update existing data associated with each edge.</span>
<span class="sd">        attr : keyword arguments, optional</span>
<span class="sd">            Edge data (or labels or objects) can be assigned using</span>
<span class="sd">            keyword arguments.</span>


<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_edge : add a single edge</span>
<span class="sd">        add_weighted_edges_from : convenient way to add weighted edges</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Adding the same edge twice has no effect but any edge data</span>
<span class="sd">        will be updated when each duplicate edge is added.</span>

<span class="sd">        Edge attributes specified in edges take precedence</span>
<span class="sd">        over attributes specified generally.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([(0,1),(1,2)]) # using a list of edge tuples</span>
<span class="sd">        &gt;&gt;&gt; e = zip(range(0,3),range(1,4))</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from(e) # Add the path graph 0-1-2-3</span>

<span class="sd">        Associate data to edges</span>

<span class="sd">        &gt;&gt;&gt; G.add_edges_from([(1,2),(2,3)], weight=3)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edges_from([(3,4),(1,4)], label=&#39;WN2898&#39;)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># set up attribute dict</span>
        <span class="k">if</span> <span class="n">attr_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">attr_dict</span> <span class="o">=</span> <span class="n">attr</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">attr_dict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span>
                    <span class="s2">&quot;The attr_dict argument must be a dictionary.&quot;</span><span class="p">)</span>
        <span class="c1"># process ebunch</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">ebunch</span><span class="p">:</span>
            <span class="n">ne</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ne</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
                <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">dd</span> <span class="o">=</span> <span class="n">e</span>
            <span class="k">elif</span> <span class="n">ne</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">u</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">e</span>
                <span class="n">dd</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># doesnt need edge_attr_dict_factory</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span>
                    <span class="s2">&quot;Edge tuple </span><span class="si">%s</span><span class="s2"> must be a 2-tuple or 3-tuple.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">e</span><span class="p">,))</span>
            <span class="k">if</span> <span class="n">u</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">if</span> <span class="n">v</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">datadict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">edge_attr_dict_factory</span><span class="p">())</span>
            <span class="n">datadict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">attr_dict</span><span class="p">)</span>
            <span class="n">datadict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">dd</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">datadict</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span> <span class="o">=</span> <span class="n">datadict</span>

    <span class="k">def</span> <span class="nf">add_weighted_edges_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ebunch</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="s1">&#39;weight&#39;</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add all the edges in ebunch as weighted edges with specified</span>
<span class="sd">        weights.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        ebunch : container of edges</span>
<span class="sd">            Each edge given in the list or container will be added</span>
<span class="sd">            to the graph. The edges must be given as 3-tuples (u,v,w)</span>
<span class="sd">            where w is a number.</span>
<span class="sd">        weight : string, optional (default= &#39;weight&#39;)</span>
<span class="sd">            The attribute name for the edge weights to be added.</span>
<span class="sd">        attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">            Edge attributes to add/update for all edges.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_edge : add a single edge</span>
<span class="sd">        add_edges_from : add multiple edges</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Adding the same edge twice for Graph/DiGraph simply updates</span>
<span class="sd">        the edge data.  For MultiGraph/MultiDiGraph, duplicate edges</span>
<span class="sd">        are stored.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_weighted_edges_from([(0,1,3.0),(1,2,7.5)])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(((</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="p">{</span><span class="n">weight</span><span class="p">:</span> <span class="n">d</span><span class="p">})</span> <span class="k">for</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">ebunch</span><span class="p">),</span> <span class="o">**</span><span class="n">attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">remove_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove the edge between u and v.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v : nodes</span>
<span class="sd">            Remove the edge between nodes u and v.</span>

<span class="sd">        Raises</span>
<span class="sd">        ------</span>
<span class="sd">        NetworkXError</span>
<span class="sd">            If there is not an edge between u and v.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        remove_edges_from : remove a collection of edges</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.remove_edge(0,1)</span>
<span class="sd">        &gt;&gt;&gt; e = (1,2)</span>
<span class="sd">        &gt;&gt;&gt; G.remove_edge(*e) # unpacks e from an edge tuple</span>
<span class="sd">        &gt;&gt;&gt; e = (2,3,{&#39;weight&#39;:7}) # an edge with attribute data</span>
<span class="sd">        &gt;&gt;&gt; G.remove_edge(*e[:2]) # select first part of edge tuple</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span><span class="p">:</span>  <span class="c1"># self-loop needs only one entry removed</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span><span class="s2">&quot;The edge </span><span class="si">%s</span><span class="s2">-</span><span class="si">%s</span><span class="s2"> is not in the graph&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">remove_edges_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ebunch</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove all edges specified in ebunch.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        ebunch: list or container of edge tuples</span>
<span class="sd">            Each edge given in the list or container will be removed</span>
<span class="sd">            from the graph. The edges can be:</span>

<span class="sd">                - 2-tuples (u,v) edge between u and v.</span>
<span class="sd">                - 3-tuples (u,v,k) where k is ignored.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        remove_edge : remove a single edge</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Will fail silently if an edge in ebunch is not in the graph.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; ebunch=[(1,2),(2,3)]</span>
<span class="sd">        &gt;&gt;&gt; G.remove_edges_from(ebunch)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">adj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span>
        <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">ebunch</span><span class="p">:</span>
            <span class="n">u</span><span class="p">,</span> <span class="n">v</span> <span class="o">=</span> <span class="n">e</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>  <span class="c1"># ignore edge data if present</span>
            <span class="k">if</span> <span class="n">u</span> <span class="ow">in</span> <span class="n">adj</span> <span class="ow">and</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]:</span>
                <span class="k">del</span> <span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">u</span> <span class="o">!=</span> <span class="n">v</span><span class="p">:</span>  <span class="c1"># self loop needs only one entry removed</span>
                    <span class="k">del</span> <span class="n">adj</span><span class="p">[</span><span class="n">v</span><span class="p">][</span><span class="n">u</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">has_edge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if the edge (u,v) is in the graph.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v : nodes</span>
<span class="sd">            Nodes can be, for example, strings or numbers.</span>
<span class="sd">            Nodes must be hashable (and not None) Python objects.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        edge_ind : bool</span>
<span class="sd">            True if edge is in the graph, False otherwise.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        Can be called either using two nodes u,v or edge tuple (u,v)</span>

<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.has_edge(0,1)  # using two nodes</span>
<span class="sd">        True</span>
<span class="sd">        &gt;&gt;&gt; e = (0,1)</span>
<span class="sd">        &gt;&gt;&gt; G.has_edge(*e)  #  e is a 2-tuple (u,v)</span>
<span class="sd">        True</span>
<span class="sd">        &gt;&gt;&gt; e = (0,1,{&#39;weight&#39;:7})</span>
<span class="sd">        &gt;&gt;&gt; G.has_edge(*e[:2])  # e is a 3-tuple (u,v,data_dictionary)</span>
<span class="sd">        True</span>

<span class="sd">        The following syntax are all equivalent:</span>

<span class="sd">        &gt;&gt;&gt; G.has_edge(0,1)</span>
<span class="sd">        True</span>
<span class="sd">        &gt;&gt;&gt; 1 in G[0]  # though this gives KeyError if 0 not in G</span>
<span class="sd">        True</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">neighbors</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a list of the nodes connected to the node n.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        n : node</span>
<span class="sd">           A node in the graph</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nlist : list</span>
<span class="sd">            A list of nodes that are adjacent to n.</span>

<span class="sd">        Raises</span>
<span class="sd">        ------</span>
<span class="sd">        NetworkXError</span>
<span class="sd">            If the node n is not in the graph.</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        It is usually more convenient (and faster) to access the</span>
<span class="sd">        adjacency dictionary as G[n]:</span>

<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(&#39;a&#39;,&#39;b&#39;,weight=7)</span>
<span class="sd">        &gt;&gt;&gt; G[&#39;a&#39;]</span>
<span class="sd">        {&#39;b&#39;: {&#39;weight&#39;: 7}}</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.neighbors(0)</span>
<span class="sd">        [1]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">])</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span><span class="s2">&quot;The node </span><span class="si">%s</span><span class="s2"> is not in the graph.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,))</span>

    <span class="k">def</span> <span class="nf">neighbors_iter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iterator over all neighbors of node n.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; [n for n in G.neighbors_iter(0)]</span>
<span class="sd">        [1]</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        It is faster to use the idiom &quot;in G[0]&quot;, e.g.</span>

<span class="sd">        &gt;&gt;&gt; G = nx.path_graph(4)</span>
<span class="sd">        &gt;&gt;&gt; [n for n in G[0]]</span>
<span class="sd">        [1]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">])</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span><span class="s2">&quot;The node </span><span class="si">%s</span><span class="s2"> is not in the graph.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,))</span>

    <span class="k">def</span> <span class="nf">edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nbunch</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a list of edges.</span>

<span class="sd">        Edges are returned as tuples with optional data</span>
<span class="sd">        in the order (node, neighbor, data).</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nbunch : iterable container, optional (default= all nodes)</span>
<span class="sd">            A container of nodes.  The container will be iterated</span>
<span class="sd">            through once.</span>
<span class="sd">        data : string or bool, optional (default=False)</span>
<span class="sd">            The edge attribute returned in 3-tuple (u,v,ddict[data]).</span>
<span class="sd">            If True, return edge attribute dict in 3-tuple (u,v,ddict).</span>
<span class="sd">            If False, return 2-tuple (u,v).</span>
<span class="sd">        default : value, optional (default=None)</span>
<span class="sd">            Value used for edges that dont have the requested attribute.</span>
<span class="sd">            Only relevant if data is not True or False.</span>

<span class="sd">        Returns</span>
<span class="sd">        --------</span>
<span class="sd">        edge_list: list of edge tuples</span>
<span class="sd">            Edges that are adjacent to any node in nbunch, or a list</span>
<span class="sd">            of all edges if nbunch is not specified.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        edges_iter : return an iterator over the edges</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Nodes in nbunch that are not in the graph will be (quietly) ignored.</span>
<span class="sd">        For directed graphs this returns the out-edges.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(2,3,weight=5)</span>
<span class="sd">        &gt;&gt;&gt; G.edges()</span>
<span class="sd">        [(0, 1), (1, 2), (2, 3)]</span>
<span class="sd">        &gt;&gt;&gt; G.edges(data=True) # default edge data is {} (empty dictionary)</span>
<span class="sd">        [(0, 1, {}), (1, 2, {}), (2, 3, {&#39;weight&#39;: 5})]</span>
<span class="sd">        &gt;&gt;&gt; list(G.edges_iter(data=&#39;weight&#39;, default=1))</span>
<span class="sd">        [(0, 1, 1), (1, 2, 1), (2, 3, 5)]</span>
<span class="sd">        &gt;&gt;&gt; G.edges([0,3])</span>
<span class="sd">        [(0, 1), (3, 2)]</span>
<span class="sd">        &gt;&gt;&gt; G.edges(0)</span>
<span class="sd">        [(0, 1)]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">edges_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">default</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">edges_iter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nbunch</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iterator over the edges.</span>

<span class="sd">        Edges are returned as tuples with optional data</span>
<span class="sd">        in the order (node, neighbor, data).</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nbunch : iterable container, optional (default= all nodes)</span>
<span class="sd">            A container of nodes.  The container will be iterated</span>
<span class="sd">            through once.</span>
<span class="sd">        data : string or bool, optional (default=False)</span>
<span class="sd">            The edge attribute returned in 3-tuple (u,v,ddict[data]).</span>
<span class="sd">            If True, return edge attribute dict in 3-tuple (u,v,ddict).</span>
<span class="sd">            If False, return 2-tuple (u,v).</span>
<span class="sd">        default : value, optional (default=None)</span>
<span class="sd">            Value used for edges that dont have the requested attribute.</span>
<span class="sd">            Only relevant if data is not True or False.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        edge_iter : iterator</span>
<span class="sd">            An iterator of (u,v) or (u,v,d) tuples of edges.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        edges : return a list of edges</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        Nodes in nbunch that are not in the graph will be (quietly) ignored.</span>
<span class="sd">        For directed graphs this returns the out-edges.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or MultiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(2,3,weight=5)</span>
<span class="sd">        &gt;&gt;&gt; [e for e in G.edges_iter()]</span>
<span class="sd">        [(0, 1), (1, 2), (2, 3)]</span>
<span class="sd">        &gt;&gt;&gt; list(G.edges_iter(data=True)) # default data is {} (empty dict)</span>
<span class="sd">        [(0, 1, {}), (1, 2, {}), (2, 3, {&#39;weight&#39;: 5})]</span>
<span class="sd">        &gt;&gt;&gt; list(G.edges_iter(data=&#39;weight&#39;, default=1))</span>
<span class="sd">        [(0, 1, 1), (1, 2, 1), (2, 3, 5)]</span>
<span class="sd">        &gt;&gt;&gt; list(G.edges_iter([0,3]))</span>
<span class="sd">        [(0, 1), (3, 2)]</span>
<span class="sd">        &gt;&gt;&gt; list(G.edges_iter(0))</span>
<span class="sd">        [(0, 1)]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">seen</span> <span class="o">=</span> <span class="p">{}</span>     <span class="c1"># helper dict to keep track of multiply stored edges</span>
        <span class="k">if</span> <span class="n">nbunch</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">nodes_nbrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nodes_nbrs</span> <span class="o">=</span> <span class="p">((</span><span class="n">n</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">])</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nbunch_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="n">nodes_nbrs</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">nbr</span><span class="p">,</span> <span class="n">ddict</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">nbr</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span>
                        <span class="k">yield</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">nbr</span><span class="p">,</span> <span class="n">ddict</span><span class="p">)</span>
                <span class="n">seen</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">elif</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">False</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="n">nodes_nbrs</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">nbr</span><span class="p">,</span> <span class="n">ddict</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">nbr</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span>
                        <span class="n">d</span> <span class="o">=</span> <span class="n">ddict</span><span class="p">[</span><span class="n">data</span><span class="p">]</span> <span class="k">if</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">ddict</span> <span class="k">else</span> <span class="n">default</span>
                        <span class="k">yield</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">nbr</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
                <span class="n">seen</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>  <span class="c1"># data is False</span>
            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="n">nodes_nbrs</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">nbr</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">nbr</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span>
                        <span class="k">yield</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">nbr</span><span class="p">)</span>
                <span class="n">seen</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">del</span> <span class="n">seen</span>

    <span class="k">def</span> <span class="nf">get_edge_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the attribute dictionary associated with edge (u,v).</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v : nodes</span>
<span class="sd">        default:  any Python object (default=None)</span>
<span class="sd">            Value to return if the edge (u,v) is not found.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        edge_dict : dictionary</span>
<span class="sd">            The edge attribute dictionary.</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        It is faster to use G[u][v].</span>

<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G[0][1]</span>
<span class="sd">        {}</span>

<span class="sd">        Warning: Assigning G[u][v] corrupts the graph data structure.</span>
<span class="sd">        But it is safe to assign attributes to that dictionary,</span>

<span class="sd">        &gt;&gt;&gt; G[0][1][&#39;weight&#39;] = 7</span>
<span class="sd">        &gt;&gt;&gt; G[0][1][&#39;weight&#39;]</span>
<span class="sd">        7</span>
<span class="sd">        &gt;&gt;&gt; G[1][0][&#39;weight&#39;]</span>
<span class="sd">        7</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.get_edge_data(0,1) # default edge data is {}</span>
<span class="sd">        {}</span>
<span class="sd">        &gt;&gt;&gt; e = (0,1)</span>
<span class="sd">        &gt;&gt;&gt; G.get_edge_data(*e) # tuple form</span>
<span class="sd">        {}</span>
<span class="sd">        &gt;&gt;&gt; G.get_edge_data(&#39;a&#39;,&#39;b&#39;,default=0) # edge not in graph, return 0</span>
<span class="sd">        0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">][</span><span class="n">v</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">default</span>

    <span class="k">def</span> <span class="nf">adjacency_list</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an adjacency list representation of the graph.</span>

<span class="sd">        The output adjacency list is in the order of G.nodes().</span>
<span class="sd">        For directed graphs, only outgoing adjacencies are included.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        adj_list : lists of lists</span>
<span class="sd">            The adjacency structure of the graph as a list of lists.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        adjacency_iter</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.adjacency_list() # in order given by G.nodes()</span>
<span class="sd">        [[1], [0, 2], [1, 3], [2]]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">values</span><span class="p">())))</span>

    <span class="k">def</span> <span class="nf">adjacency_iter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iterator of (node, adjacency dict) tuples for all nodes.</span>

<span class="sd">        This is the fastest way to look at every edge.</span>
<span class="sd">        For directed graphs, only outgoing adjacencies are included.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        adj_iter : iterator</span>
<span class="sd">           An iterator of (node, adjacency dictionary) for all nodes in</span>
<span class="sd">           the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        adjacency_list</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; [(n,nbrdict) for n,nbrdict in G.adjacency_iter()]</span>
<span class="sd">        [(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">degree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nbunch</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the degree of a node or nodes.</span>

<span class="sd">        The node degree is the number of edges adjacent to that node.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nbunch : iterable container, optional (default=all nodes)</span>
<span class="sd">            A container of nodes.  The container will be iterated</span>
<span class="sd">            through once.</span>

<span class="sd">        weight : string or None, optional (default=None)</span>
<span class="sd">           The edge attribute that holds the numerical value used</span>
<span class="sd">           as a weight.  If None, then each edge has weight 1.</span>
<span class="sd">           The degree is the sum of the edge weights adjacent to the node.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nd : dictionary, or number</span>
<span class="sd">            A dictionary with nodes as keys and degree as values or</span>
<span class="sd">            a number if a single node is specified.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.degree(0)</span>
<span class="sd">        1</span>
<span class="sd">        &gt;&gt;&gt; G.degree([0,1])</span>
<span class="sd">        {0: 1, 1: 2}</span>
<span class="sd">        &gt;&gt;&gt; list(G.degree([0,1]).values())</span>
<span class="sd">        [1, 2]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">nbunch</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>      <span class="c1"># return a single node</span>
            <span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">degree_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">,</span> <span class="n">weight</span><span class="p">))[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>           <span class="c1"># return a dict</span>
            <span class="k">return</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">degree_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">,</span> <span class="n">weight</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">degree_iter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nbunch</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iterator for (node, degree).</span>

<span class="sd">        The node degree is the number of edges adjacent to the node.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nbunch : iterable container, optional (default=all nodes)</span>
<span class="sd">            A container of nodes.  The container will be iterated</span>
<span class="sd">            through once.</span>

<span class="sd">        weight : string or None, optional (default=None)</span>
<span class="sd">           The edge attribute that holds the numerical value used</span>
<span class="sd">           as a weight.  If None, then each edge has weight 1.</span>
<span class="sd">           The degree is the sum of the edge weights adjacent to the node.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nd_iter : an iterator</span>
<span class="sd">            The iterator returns two-tuples of (node, degree).</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        degree</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; list(G.degree_iter(0)) # node 0 with degree 1</span>
<span class="sd">        [(0, 1)]</span>
<span class="sd">        &gt;&gt;&gt; list(G.degree_iter([0,1]))</span>
<span class="sd">        [(0, 1), (1, 2)]</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">nbunch</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">nodes_nbrs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nodes_nbrs</span> <span class="o">=</span> <span class="p">((</span><span class="n">n</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">n</span><span class="p">])</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">nbunch_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">))</span>

        <span class="k">if</span> <span class="n">weight</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="n">nodes_nbrs</span><span class="p">:</span>
                <span class="k">yield</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">nbrs</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">n</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">))</span>  <span class="c1"># return tuple (n,degree)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># edge weighted graph - degree is sum of nbr edge weights</span>
            <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="n">nodes_nbrs</span><span class="p">:</span>
                <span class="k">yield</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">sum</span><span class="p">((</span><span class="n">nbrs</span><span class="p">[</span><span class="n">nbr</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">weight</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">nbr</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">))</span> <span class="o">+</span>
                    <span class="p">(</span><span class="n">n</span> <span class="ow">in</span> <span class="n">nbrs</span> <span class="ow">and</span> <span class="n">nbrs</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">weight</span><span class="p">,</span> <span class="mi">1</span><span class="p">)))</span>

    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Remove all nodes and edges from the graph.</span>

<span class="sd">        This also removes the name, and all graph, node, and edge attributes.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.clear()</span>
<span class="sd">        &gt;&gt;&gt; G.nodes()</span>
<span class="sd">        []</span>
<span class="sd">        &gt;&gt;&gt; G.edges()</span>
<span class="sd">        []</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a copy of the graph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        G : Graph</span>
<span class="sd">            A copy of the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        to_directed: return a directed copy of the graph.</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        This makes a complete copy of the graph including all of the</span>
<span class="sd">        node or edge attributes.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; H = G.copy()</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_multigraph</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if graph is a multigraph, False otherwise.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">is_directed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return True if graph is directed, False otherwise.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">to_directed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a directed representation of the graph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        G : DiGraph</span>
<span class="sd">            A directed graph with the same name, same nodes, and with</span>
<span class="sd">            each edge (u,v,data) replaced by two directed edges</span>
<span class="sd">            (u,v,data) and (v,u,data).</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        This returns a &quot;deepcopy&quot; of the edge, node, and</span>
<span class="sd">        graph attributes which attempts to completely copy</span>
<span class="sd">        all of the data and references.</span>

<span class="sd">        This is in contrast to the similar D=DiGraph(G) which returns a</span>
<span class="sd">        shallow copy of the data.</span>

<span class="sd">        See the Python copy module for more information on shallow</span>
<span class="sd">        and deep copies, http://docs.python.org/library/copy.html.</span>

<span class="sd">        Warning: If you have subclassed Graph to use dict-like objects in the</span>
<span class="sd">        data structure, those changes do not transfer to the DiGraph</span>
<span class="sd">        created by this method.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or MultiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1])</span>
<span class="sd">        &gt;&gt;&gt; H = G.to_directed()</span>
<span class="sd">        &gt;&gt;&gt; H.edges()</span>
<span class="sd">        [(0, 1), (1, 0)]</span>

<span class="sd">        If already directed, return a (deep) copy</span>

<span class="sd">        &gt;&gt;&gt; G = nx.DiGraph()   # or MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1])</span>
<span class="sd">        &gt;&gt;&gt; H = G.to_directed()</span>
<span class="sd">        &gt;&gt;&gt; H.edges()</span>
<span class="sd">        [(0, 1)]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="kn">from</span> <span class="nn">networkx</span> <span class="k">import</span> <span class="n">DiGraph</span>
        <span class="n">G</span> <span class="o">=</span> <span class="n">DiGraph</span><span class="p">()</span>
        <span class="n">G</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="n">G</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">G</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(((</span><span class="n">u</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">deepcopy</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
            <span class="k">for</span> <span class="n">u</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adjacency_iter</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">v</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="o">.</span><span class="n">items</span><span class="p">()))</span>
        <span class="n">G</span><span class="o">.</span><span class="n">graph</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">graph</span><span class="p">)</span>
        <span class="n">G</span><span class="o">.</span><span class="n">node</span> <span class="o">=</span> <span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">G</span>

    <span class="k">def</span> <span class="nf">to_undirected</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an undirected copy of the graph.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        G : Graph/MultiGraph</span>
<span class="sd">            A deepcopy of the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        copy, add_edge, add_edges_from</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        This returns a &quot;deepcopy&quot; of the edge, node, and</span>
<span class="sd">        graph attributes which attempts to completely copy</span>
<span class="sd">        all of the data and references.</span>

<span class="sd">        This is in contrast to the similar G=DiGraph(D) which returns a</span>
<span class="sd">        shallow copy of the data.</span>

<span class="sd">        See the Python copy module for more information on shallow</span>
<span class="sd">        and deep copies, http://docs.python.org/library/copy.html.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or MultiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1])</span>
<span class="sd">        &gt;&gt;&gt; H = G.to_directed()</span>
<span class="sd">        &gt;&gt;&gt; H.edges()</span>
<span class="sd">        [(0, 1), (1, 0)]</span>
<span class="sd">        &gt;&gt;&gt; G2 = H.to_undirected()</span>
<span class="sd">        &gt;&gt;&gt; G2.edges()</span>
<span class="sd">        [(0, 1)]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">deepcopy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">subgraph</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nbunch</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the subgraph induced on nodes in nbunch.</span>

<span class="sd">        The induced subgraph of the graph contains the nodes in nbunch</span>
<span class="sd">        and the edges between those nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nbunch : list, iterable</span>
<span class="sd">            A container of nodes which will be iterated through once.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        G : Graph</span>
<span class="sd">            A subgraph of the graph with the same edge attributes.</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        The graph, edge or node attributes just point to the original graph.</span>
<span class="sd">        So changes to the node or edge structure will not be reflected in</span>
<span class="sd">        the original graph while changes to the attributes will.</span>

<span class="sd">        To create a subgraph with its own copy of the edge/node attributes use:</span>
<span class="sd">        nx.Graph(G.subgraph(nbunch))</span>

<span class="sd">        If edge attributes are containers, a deep copy can be obtained using:</span>
<span class="sd">        G.subgraph(nbunch).copy()</span>

<span class="sd">        For an inplace reduction of a graph to a subgraph you can remove nodes:</span>
<span class="sd">        G.remove_nodes_from([ n in G if n not in set(nbunch)])</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; H = G.subgraph([0,1,2])</span>
<span class="sd">        &gt;&gt;&gt; H.edges()</span>
<span class="sd">        [(0, 1), (1, 2)]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bunch</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nbunch_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">)</span>
        <span class="c1"># create new graph and copy subgraph into it</span>
        <span class="n">H</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="p">()</span>
        <span class="c1"># copy node and attribute dictionaries</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">bunch</span><span class="p">:</span>
            <span class="n">H</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">node</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
        <span class="c1"># namespace shortcuts for speed</span>
        <span class="n">H_adj</span> <span class="o">=</span> <span class="n">H</span><span class="o">.</span><span class="n">adj</span>
        <span class="n">self_adj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span>
        <span class="c1"># add nodes and edges (undirected method)</span>
        <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">H</span><span class="o">.</span><span class="n">node</span><span class="p">:</span>
            <span class="n">Hnbrs</span> <span class="o">=</span> <span class="n">H</span><span class="o">.</span><span class="n">adjlist_dict_factory</span><span class="p">()</span>
            <span class="n">H_adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">Hnbrs</span>
            <span class="k">for</span> <span class="n">nbr</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">self_adj</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">nbr</span> <span class="ow">in</span> <span class="n">H_adj</span><span class="p">:</span>
                    <span class="c1"># add both representations of edge: n-nbr and nbr-n</span>
                    <span class="n">Hnbrs</span><span class="p">[</span><span class="n">nbr</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span>
                    <span class="n">H_adj</span><span class="p">[</span><span class="n">nbr</span><span class="p">][</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span>
        <span class="n">H</span><span class="o">.</span><span class="n">graph</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">graph</span>
        <span class="k">return</span> <span class="n">H</span>

    <span class="k">def</span> <span class="nf">nodes_with_selfloops</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a list of nodes with self loops.</span>

<span class="sd">        A node with a self loop has an edge with both ends adjacent</span>
<span class="sd">        to that node.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nodelist : list</span>
<span class="sd">            A list of nodes with self loops.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        selfloop_edges, number_of_selfloops</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1,1)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1,2)</span>
<span class="sd">        &gt;&gt;&gt; G.nodes_with_selfloops()</span>
<span class="sd">        [1]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">n</span> <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">selfloop_edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a list of selfloop edges.</span>

<span class="sd">        A selfloop edge has the same node at both ends.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data : string or bool, optional (default=False)</span>
<span class="sd">            Return selfloop edges as two tuples (u,v) (data=False)</span>
<span class="sd">            or three-tuples (u,v,datadict) (data=True)</span>
<span class="sd">            or three-tuples (u,v,datavalue) (data=&#39;attrname&#39;)</span>
<span class="sd">        default : value, optional (default=None)</span>
<span class="sd">            Value used for edges that dont have the requested attribute.</span>
<span class="sd">            Only relevant if data is not True or False.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        edgelist : list of edge tuples</span>
<span class="sd">            A list of all selfloop edges.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        nodes_with_selfloops, number_of_selfloops</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1,1)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1,2)</span>
<span class="sd">        &gt;&gt;&gt; G.selfloop_edges()</span>
<span class="sd">        [(1, 1)]</span>
<span class="sd">        &gt;&gt;&gt; G.selfloop_edges(data=True)</span>
<span class="sd">        [(1, 1, {})]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span><span class="p">[</span><span class="n">n</span><span class="p">])</span>
                    <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">False</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">default</span><span class="p">))</span>
                    <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">nbrs</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nbrs</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">number_of_selfloops</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of selfloop edges.</span>

<span class="sd">        A selfloop edge has the same node at both ends.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nloops : int</span>
<span class="sd">            The number of selfloops.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        nodes_with_selfloops, selfloop_edges</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1,1)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(1,2)</span>
<span class="sd">        &gt;&gt;&gt; G.number_of_selfloops()</span>
<span class="sd">        1</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">selfloop_edges</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of edges.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        weight : string or None, optional (default=None)</span>
<span class="sd">           The edge attribute that holds the numerical value used</span>
<span class="sd">           as a weight.  If None, then each edge has weight 1.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nedges : int</span>
<span class="sd">            The number of edges or sum of edge weights in the graph.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        number_of_edges</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.size()</span>
<span class="sd">        3</span>

<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(&#39;a&#39;,&#39;b&#39;,weight=2)</span>
<span class="sd">        &gt;&gt;&gt; G.add_edge(&#39;b&#39;,&#39;c&#39;,weight=4)</span>
<span class="sd">        &gt;&gt;&gt; G.size()</span>
<span class="sd">        2</span>
<span class="sd">        &gt;&gt;&gt; G.size(weight=&#39;weight&#39;)</span>
<span class="sd">        6.0</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">degree</span><span class="p">(</span><span class="n">weight</span><span class="o">=</span><span class="n">weight</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="o">/</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="n">weight</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">number_of_edges</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">u</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">v</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the number of edges between two nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        u, v : nodes, optional (default=all edges)</span>
<span class="sd">            If u and v are specified, return the number of edges between</span>
<span class="sd">            u and v. Otherwise return the total number of all edges.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        nedges : int</span>
<span class="sd">            The number of edges in the graph.  If nodes u and v are specified</span>
<span class="sd">            return the number of edges between those nodes.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        size</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.number_of_edges()</span>
<span class="sd">        3</span>
<span class="sd">        &gt;&gt;&gt; G.number_of_edges(0,1)</span>
<span class="sd">        1</span>
<span class="sd">        &gt;&gt;&gt; e = (0,1)</span>
<span class="sd">        &gt;&gt;&gt; G.number_of_edges(*e)</span>
<span class="sd">        1</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">u</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">[</span><span class="n">u</span><span class="p">]:</span>
            <span class="k">return</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">add_star</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a star.</span>

<span class="sd">        The first node in nodes is the middle of the star.  It is connected</span>
<span class="sd">        to all other nodes.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nodes : iterable container</span>
<span class="sd">            A container of nodes.</span>
<span class="sd">        attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">            Attributes to add to every edge in star.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_path, add_cycle</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G = nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_star([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.add_star([10,11,12],weight=2)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nlist</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">nodes</span><span class="p">)</span>
        <span class="n">v</span> <span class="o">=</span> <span class="n">nlist</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">edges</span> <span class="o">=</span> <span class="p">((</span><span class="n">v</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nlist</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">edges</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a path.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nodes : iterable container</span>
<span class="sd">            A container of nodes.  A path will be constructed from</span>
<span class="sd">            the nodes (in order) and added to the graph.</span>
<span class="sd">        attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">            Attributes to add to every edge in path.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_star, add_cycle</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.add_path([10,11,12],weight=7)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nlist</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">nodes</span><span class="p">)</span>
        <span class="n">edges</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">nlist</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">nlist</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">edges</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">add_cycle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nodes</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add a cycle.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nodes: iterable container</span>
<span class="sd">            A container of nodes.  A cycle will be constructed from</span>
<span class="sd">            the nodes (in order) and added to the graph.</span>
<span class="sd">        attr : keyword arguments, optional (default= no attributes)</span>
<span class="sd">            Attributes to add to every edge in cycle.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        add_path, add_star</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; G=nx.Graph()   # or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="sd">        &gt;&gt;&gt; G.add_cycle([0,1,2,3])</span>
<span class="sd">        &gt;&gt;&gt; G.add_cycle([10,11,12],weight=7)</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">nlist</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">nodes</span><span class="p">)</span>
        <span class="n">edges</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">nlist</span><span class="p">,</span> <span class="n">nlist</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="o">+</span> <span class="p">[</span><span class="n">nlist</span><span class="p">[</span><span class="mi">0</span><span class="p">]])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">edges</span><span class="p">,</span> <span class="o">**</span><span class="n">attr</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">nbunch_iter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nbunch</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an iterator of nodes contained in nbunch that are</span>
<span class="sd">        also in the graph.</span>

<span class="sd">        The nodes in nbunch are checked for membership in the graph</span>
<span class="sd">        and if not are silently ignored.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        nbunch : iterable container, optional (default=all nodes)</span>
<span class="sd">            A container of nodes.  The container will be iterated</span>
<span class="sd">            through once.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        niter : iterator</span>
<span class="sd">            An iterator over nodes in nbunch that are also in the graph.</span>
<span class="sd">            If nbunch is None, iterate over all nodes in the graph.</span>

<span class="sd">        Raises</span>
<span class="sd">        ------</span>
<span class="sd">        NetworkXError</span>
<span class="sd">            If nbunch is not a node or or sequence of nodes.</span>
<span class="sd">            If a node in nbunch is not hashable.</span>

<span class="sd">        See Also</span>
<span class="sd">        --------</span>
<span class="sd">        Graph.__iter__</span>

<span class="sd">        Notes</span>
<span class="sd">        -----</span>
<span class="sd">        When nbunch is an iterator, the returned iterator yields values</span>
<span class="sd">        directly from nbunch, becoming exhausted when nbunch is exhausted.</span>

<span class="sd">        To test whether nbunch is a single node, one can use</span>
<span class="sd">        &quot;if nbunch in self:&quot;, even after processing with this routine.</span>

<span class="sd">        If nbunch is not a node or a (possibly empty) sequence/iterator</span>
<span class="sd">        or None, a NetworkXError is raised.  Also, if any object in</span>
<span class="sd">        nbunch is not hashable, a NetworkXError is raised.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">nbunch</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>   <span class="c1"># include all nodes via iterator</span>
            <span class="n">bunch</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="k">elif</span> <span class="n">nbunch</span> <span class="ow">in</span> <span class="bp">self</span><span class="p">:</span>  <span class="c1"># if nbunch is a single node</span>
            <span class="n">bunch</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">([</span><span class="n">nbunch</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>                <span class="c1"># if nbunch is a sequence of nodes</span>
            <span class="k">def</span> <span class="nf">bunch_iter</span><span class="p">(</span><span class="n">nlist</span><span class="p">,</span> <span class="n">adj</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nlist</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">adj</span><span class="p">:</span>
                            <span class="k">yield</span> <span class="n">n</span>
                <span class="k">except</span> <span class="ne">TypeError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="n">message</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="c1"># capture error for non-sequence/iterator nbunch.</span>
                    <span class="k">if</span> <span class="s1">&#39;iter&#39;</span> <span class="ow">in</span> <span class="n">message</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span>
                            <span class="s2">&quot;nbunch is not a node or a sequence of nodes.&quot;</span><span class="p">)</span>
                    <span class="c1"># capture error for unhashable node.</span>
                    <span class="k">elif</span> <span class="s1">&#39;hashable&#39;</span> <span class="ow">in</span> <span class="n">message</span><span class="p">:</span>
                        <span class="k">raise</span> <span class="n">NetworkXError</span><span class="p">(</span>
                            <span class="s2">&quot;Node </span><span class="si">%s</span><span class="s2"> in the sequence nbunch is not a valid node.&quot;</span><span class="o">%</span><span class="n">n</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">raise</span>
            <span class="n">bunch</span> <span class="o">=</span> <span class="n">bunch_iter</span><span class="p">(</span><span class="n">nbunch</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">adj</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">bunch</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>